home *** CD-ROM | disk | FTP | other *** search
- /*+
- * File: hxd
- * Description:
- * hxd, Hexadecimal dump is similar unix's "od -x"
- * Displays the contents of a file in hexadecimal.
- * If a byte is printable, it is also printed.
- * format of output is :
- * dec_add:hex_add word word word word word word word word string
- * where:
- * dec_add == Start address for that line in decimal.
- * hex_add == Start address for that line in hex.
- * word == Representation of two bytes in hex. Two characters
- * for each byte. First two characters in "word" represent
- * the byte that was read sooner.
- * string == If the character is printable, it is printed.
- * Otherwise a '.' is printed. '.'is printed as '.'.
- * For each in put file name of the file is first emited.
- * file: <i_file>
- * last line for each input file is length of the file in
- * "dec_add:hex_add" format.
- *
- * Usages:
- * hxd [-o] <o_file> <i_files>
- *
- *
- * Author: Mohsen Banan.
- *
- * This program is public domain software, no warranty intended or
- * implied.
- * General permission to copy or modify, but not for profit, is
- * hereby granted.
- *
- *
- * Functions:
- *
- *
- * Audit Trail: $Log: hxd.c,v $
- * Revision 1.1 85/08/28 08:38:36 mohsen
- * original posting to the net
- *
- *
- *
- -*/
-
- #ifdef RCS_VER
- static char *rcs = "$Header: hxd.c,v 1.1 85/08/28 08:38:36 mohsen Exp $";
- #endif
-
- /* #includes */
- #include "mbstd.h"
- #include <stdio.h>
- #include <ctype.h>
- #include <fcntl.h>
- #ifdef unix
- #include "msc3.h"
- #endif
-
- /* #defines */
-
- /* external variables */
-
- /* referenced external function declarations */
-
- /* internal function declarations */
- PUBLIC VOID hxd();
- PUBLIC VOID hxdump();
- PUBLIC VOID cant_open();
- PUBLIC VOID printisc();
- STATIC VOID usage();
-
- /* global variables */
-
- /* static variables */
- STATIC CHAR * prog_name;
-
- INT
- main (argc, argv)
- INT argc;
- CHAR * argv[];
- {
- hxd(argc, argv);
- }
-
-
- /*<
- * Function:hxd
- * Description:
- * Parses the command line and calls hxdump.
- *
- * Returns:
- * VOID
- *
- >*/
- PUBLIC VOID
- hxd (argc,argv)
- INT argc;
- CHAR * argv[];
- {
- FILE * i_fp;
- FILE * o_fp;
- INT i;
-
- prog_name = argv[0];
- i_fp = stdin;
- o_fp = stdout;
- for (i=1; i<argc; ++i) {
- if (*argv[i] == '-') {
- /* To handle concatenated switches */
- INT j;
- j=i;
- while (*(++argv[j])) {
- switch (*argv[j]) {
- case 'o':
- case 'O':
- if ( !(o_fp = fopen(argv[++i], "w")) ) {
- cant_open(prog_name, argv[i]);
- exit(1);
- }
- break;
- default:
- usage();
- exit(1);
- } /* switch (*argv[j]) */
- } /* while (*(++argv[j])) */
- } /* if '-' */
- else {
- if (!(i_fp = fopen (argv[i], "r"))) {
- cant_open (prog_name, argv[i]);
- exit (1);
- }
- fprintf (o_fp, "file: %s", argv[i]);
- setmode (fileno(i_fp), O_BINARY);
- setmode (fileno(o_fp), O_TEXT);
- hxdump (i_fp, o_fp);
- fclose (i_fp);
- }
- } /* for i<argc */
- fclose (o_fp);
- exit (0);
- }
-
-
- /*<
- * Function:HX_DUMP
- * Description:
- *
- * Arguments:
- *
- * Returns:
- *
- * Side Effects:
- *
- * Calls:
- *
- >*/
- PUBLIC VOID
- hxdump (i_fp, o_fp)
- FILE * i_fp;
- FILE * o_fp;
- {
- #define BYTESPERLINE 16
- LONG i = 0L;
- INT c;
- INT chars[BYTESPERLINE];
- INT j=0;
- INT k;
-
- while ((c = getc (i_fp)) != EOF) {
- if (! (i % BYTESPERLINE)) {
- if (i) {
- j=0;
- fprintf (o_fp, " ");
- printisc (o_fp,chars, BYTESPERLINE);
- }
-
- fprintf (o_fp, "\n%07ld:%07lx", i,i );
- }
- if (!(i++ & 1)) {
- putc (' ', o_fp);
- }
- fprintf (o_fp, "%02x", c);
- chars[j++] = c;
- }
- if (k = (i % BYTESPERLINE)) {
- INT jj;
- k = BYTESPERLINE - k;
- for (jj=0; jj < (5*k/2) ;++jj) {
- putc(' ', o_fp);
- }
- }
- fprintf (o_fp, " ");
- printisc (o_fp, chars, BYTESPERLINE);
- fprintf (o_fp, "\n%07ld:%07lx\n", i,i);
- }
-
- PUBLIC VOID
- cant_open (prog_name,filename)
- CHAR * prog_name;
- CHAR * filename;
- {
- fprintf (stderr, "%s :can not open %s \n", prog_name, filename);
- }
-
- STATIC VOID
- usage ()
- {
- fprintf (stderr, "Usage: %s [-o] <o_file> <i_file> \n", prog_name);
-
- }
-
-
- PUBLIC VOID
- printisc (o_fp, chars, length)
- FILE * o_fp;
- INT chars[];
- INT length;
- {
- INT i;
-
- for (i=0; i<length; ++i) {
- if (isprint (chars[i])) {
- putc (chars[i], o_fp);
- } else {
- putc ('.', o_fp);
- }
- }
- }
-